package com.zjjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.mysql.cj.jdbc.ha.RandomBalanceStrategy;
import com.zjjh.entity.Userinfo;
import com.zjjh.service.UserinfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/base")
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/add")
    Object add(@RequestBody Userinfo userinfo) {
        String id = IdWorker.getIdStr();
        String code = RandomStringUtils.randomNumeric(8) + StringUtils.substring(id,15);
        userinfo.setAvatar("123123213");
        userinfo.setGender(1).setId(id).setIdCode(code).setDeleted(0);
        userinfoService.save(userinfo);
        return "ok";
    }

    @GetMapping("/get")
    Object get(@RequestParam(required = false) String id,@RequestParam String mobile) {
        if (StringUtils.isNoneEmpty(mobile)){
            return userinfoService.getByMobile(mobile);
        }
        return userinfoService.getById(id);
    }

    @GetMapping("/getList")
    Object getList(
//            @RequestHeader("userId") Long userId
    ) {
        List<String> a = new ArrayList<>();
        a.add("1550353684061499393");
        a.add("1550354218277416963");
        a.add("1550353683671429122");
        return userinfoService.listByIds(a);
    }

    @GetMapping("/query")
    Object query() {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("realname","213213");
        return userinfoService.list(queryWrapper);
    }

    @GetMapping("/getByCode")
    Object getByCode() {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_code","405057534035");
        return userinfoService.getOne(queryWrapper);
    }

}
